package com.tim.app.ui.activity;

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.application.library.base.BaseViewInterface;
import com.application.library.dialog.LoadingDialog;
import com.application.library.runtime.ActivityManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.tim.app.manager.WindowUtils;
import com.umeng.analytics.MobclickAgent;


public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, BaseViewInterface {

    public static final String TAG = "BaseActivity";
    private LoadingDialog mLoadingDialog;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.ins().addActivity(this);
        onBeforeSetContentLayout();
        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
        }
        init(savedInstanceState);
        initView();
        initData();
    }

    protected void onBeforeSetContentLayout() {
    }

    protected void init(Bundle savedInstanceState) {
    }

    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.ins().finishActivity(this);
        OkHttpUtils.getInstance().cancelTag(TAG);
    }

    protected void showLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.show();
    }

    protected void showLoadingDialog(DialogInterface.OnDismissListener dismissListener) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.setOnDismissListener(dismissListener);
        mLoadingDialog.show();
    }

    protected void showLoadingDialog(int resid) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.setContent(getResources().getString(resid));
        if(!this.isFinishing()){
            mLoadingDialog.show();
        }
    }


    protected void hideLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageStart(getClass().getName());
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
       WindowUtils.getInStance().setContext(this);
        MobclickAgent.onPageEnd(getClass().getName());
        MobclickAgent.onResume(this);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }
}
